<?php
class NItem extends NActiveRecord
{
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{item}}';
    }

    public function relations()
    {
        return array(
            'category' => array(self::BELONGS_TO, 'NCategory', 'cid', 'select' => 'name')
        );
    }

    public function rules()
    {
        return array(
            array('name, url, cid', 'required'),
            array('name', 'length', 'max' => 20),
            array('url', 'length', 'max' => 128),
            array('ord', 'numerical')
        );
    }

    public function search($pageSize = 10)
    {
        $criteria = new CDbCriteria();
        $criteria->with = array('category');
        $criteria->compare('cid', $this->cid);
        $criteria->compare('t.name', $this->name, true);
        $criteria->compare('url', $this->url, true);
        $dataProvider = new CActiveDataProvider(__CLASS__, array(
            'pagination' => array(
                'pagesize' => $pageSize
            ),
            'criteria' => $criteria,
        ));

        return $dataProvider;
    }
}